package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.fegin.ProductService;
import com.example.model.Order;
import com.example.model.Product;
import com.example.service.OrderService;
import com.example.utils.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@Slf4j
@RestController
@RequestMapping(value = "order", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProductService productService;
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public RestResult getById(@RequestParam String id) {

        Order order = orderService.getById(id);
        return RestResult.ok(order);
    }
    //准备买1件商品
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
//从nacos中获取服务地址
        ServiceInstance serviceInstance =
                discoveryClient.getInstances("boot-product").get(0);
        String url = serviceInstance.getHost() + ":" +
                serviceInstance.getPort();
        log.info(">>从nacos中获取到的微服务地址为:" + url);
//通过restTemplate调用商品微服务
        String url1 ="http://" + url + "/product/product/getById/" + pid;
        log.info("微服务地址为:" + url1);
                Product product = restTemplate.getForObject(
                        url1, Product.class);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.save(order);
        return order;

    }
    //准备买1件商品
    @GetMapping("/order1/prod/{pid}")
    public Order order1(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
//直接使用微服务名字， 从nacos中获取服务地址
        String url = "boot-product";
//通过restTemplate调用商品微服务

        String url1 ="http://" + url + "/product/product/getById/" + pid;

        Product product = restTemplate.getForObject(
                url1, Product.class);
        log.info(">>商品信息，查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.save(order);
        return order;
    }

    //准备买1件商品
    @GetMapping("/order2/prod/{pid}")
    public Order order2(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单,这时候要调用商品微服务查询商品信息");
//通过fegin调用商品微服务
        Product product = productService.findByPid(pid);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.save(order);
        return order;
    }
}
